<?php

namespace app\admin\model\ykjp\sell;

use think\Model;
use traits\model\SoftDelete;

class Delivery extends Model {

    // use SoftDelete;
    // 表名
    protected $name = 'ykjp_delivery';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // protected $deletetime = 'deletetime';
    // 追加属性
    protected $append = [
        'billing_time_text',
        'delivery_time_text',
        'type_text',
        'status_list_text'
    ];

    public function getTypeList() {
        return ['1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3')];
    }

    public function getStatusListList() {
        return ['1' => __('Status_list 1'), '2' => __('Status_list 2'), '3' => __('Status_list 3'), '4' => __('Status_list 4'), '5' => __('Status_list 5')];
    }

    public function getBillingTimeTextAttr($value, $data) {
        $value = $value ? $value : (isset($data['billing_time']) ? $data['billing_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getDeliveryTimeTextAttr($value, $data) {
        $value = $value ? $value : (isset($data['delivery_time']) ? $data['delivery_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getTypeTextAttr($value, $data) {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusListTextAttr($value, $data) {
        $value = $value ? $value : (isset($data['status_list']) ? $data['status_list'] : '');
        $list = $this->getStatusListList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setBillingTimeAttr($value) {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDeliveryTimeAttr($value) {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function object_array($array) {
        if (is_object($array)) {
            $array = (array) $array;
        } if (is_array($array)) {
            foreach ($array as $key => $value) {
                $array[$key] = $this->object_array($value);
            }
        }
        return $array;
    }

}
